package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HousePublisInfoContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HouseInfoTitleBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePublishResultBean;
import com.kmproject.haifu.mvp.model.entity.resbean.HouseConfigBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/01/2019 15:42
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HousePublisInfoPresenter extends BasePresenter<HousePublisInfoContract.Model, HousePublisInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HousePublisInfoPresenter(HousePublisInfoContract.Model model, HousePublisInfoContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 更新信息
     * @param mpas
     */
    public void updateHouseRent(Map<String,Object> mpas)
    {
        mModel.updateHouseRent(mpas).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                      if (AppUtil.isSuccess(resultBean))
                      {
                          mRootView.updateHouseRentSuccess();
                      }else {
                          mRootView.showMessage(resultBean.getMessage());
                      }
                    }
                });
    }

    /**
     * 实体类转Map
     * @param object
     * @return
     */
    public  Map<String, Object> entityToMap(Object object) {
        Map<String, Object> map = new HashMap();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                if (o!=null)
                {
                    map.put(field.getName(), o);
                }

                field.setAccessible(flag);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

   public  List<HouseInfoTitleBean> getData(List<HouseInfoTitleBean.HouseInfoConfiguration> houseInfoConfigurations){
        if (houseInfoConfigurations==null||houseInfoConfigurations.isEmpty())
        {
            return null;
        }
        List<HouseInfoTitleBean> houseInfoTitleBeans=new ArrayList<>();
       HouseInfoTitleBean houseInfoTitleBean1=new HouseInfoTitleBean("房屋配置");
       HouseInfoTitleBean houseInfoTitleBean2=new HouseInfoTitleBean("房屋亮点");
       HouseInfoTitleBean houseInfoTitleBean3=new HouseInfoTitleBean("出租要求");
       List<HouseInfoTitleBean.HouseInfoConfiguration> houseInfoConfigurations1=new ArrayList<>();
       List<HouseInfoTitleBean.HouseInfoConfiguration> houseInfoConfigurations2=new ArrayList<>();
       List<HouseInfoTitleBean.HouseInfoConfiguration> houseInfoConfigurations3=new ArrayList<>();
        for (HouseInfoTitleBean.HouseInfoConfiguration houseInfoConfiguration:houseInfoConfigurations)
        {
            if ("1".equals(houseInfoConfiguration.getCategory()))
            {
                houseInfoConfigurations1.add(houseInfoConfiguration);
            }else  if ("2".equals(houseInfoConfiguration.getCategory())){
                houseInfoConfigurations2.add(houseInfoConfiguration);
            }else  if ("3".equals(houseInfoConfiguration.getCategory())){
                houseInfoConfigurations3.add(houseInfoConfiguration);
            }
        }
        houseInfoTitleBean1.setChileds(houseInfoConfigurations1);
       houseInfoTitleBean2.setChileds(houseInfoConfigurations2);
       houseInfoTitleBean3.setChileds(houseInfoConfigurations3);
       houseInfoTitleBeans.add(houseInfoTitleBean1);
       houseInfoTitleBeans.add(houseInfoTitleBean2);
       houseInfoTitleBeans.add(houseInfoTitleBean3);
       return houseInfoTitleBeans;
    }


    /**
     * 获取用户上传使用房屋配置
     * @param houseInfoTitleBeans
     * @return
     */
    public List<HouseConfigBean> getHouseConfig(List<HouseInfoTitleBean> houseInfoTitleBeans){
        if (houseInfoTitleBeans==null||houseInfoTitleBeans.isEmpty())
        {
            return null;
        }
        List<HouseConfigBean> houseConfigBeans=new ArrayList<>();
        HouseConfigBean houseConfigBean;
        for (HouseInfoTitleBean houseInfoTitleBean:houseInfoTitleBeans)
        {
          if (houseInfoTitleBean.getTitleName().equals("房屋配置")&&!houseInfoTitleBean.getChileds().isEmpty())
          {
              for (HouseInfoTitleBean.HouseInfoConfiguration houseInfoConfiguration:houseInfoTitleBean.getChileds())
              {
                  if (houseInfoConfiguration.isSelect())
                  {
                      houseConfigBean=new HouseConfigBean("1",houseInfoConfiguration.getDictId());
                      houseConfigBeans.add(houseConfigBean);
                  }
              }
          }else if (houseInfoTitleBean.getTitleName().equals("房屋亮点")&&!houseInfoTitleBean.getChileds().isEmpty())
          {
              for (HouseInfoTitleBean.HouseInfoConfiguration houseInfoConfiguration:houseInfoTitleBean.getChileds())
              {
                  if (houseInfoConfiguration.isSelect())
                  {
                      houseConfigBean=new HouseConfigBean("2",houseInfoConfiguration.getDictId());
                      houseConfigBeans.add(houseConfigBean);
                  }
              }
          }else if (houseInfoTitleBean.getTitleName().equals("出租要求")&&!houseInfoTitleBean.getChileds().isEmpty()){
              for (HouseInfoTitleBean.HouseInfoConfiguration houseInfoConfiguration:houseInfoTitleBean.getChileds())
              {
                  if (houseInfoConfiguration.isSelect())
                  {
                      houseConfigBean=new HouseConfigBean("3",houseInfoConfiguration.getDictId());
                      houseConfigBeans.add(houseConfigBean);
                  }
              }
          }
        }
        return houseConfigBeans;
    }


    /**
     * 发布
     * @param params
     */
    public void  addHouseRent(Map<String, Object> params) {
        mModel.addHouseRent(params).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                       if (AppUtil.isSuccess(resultBean))
                       {
//                           mRootView.showMessage("发布成功");
                           mRootView.addHouseRentSuccess();
                       }else {
                           mRootView.showMessage(resultBean.getMessage());
                       }
                    }
                });
    }


    /**
     * 获取配置项
     * @param
     * @param value
     */
    public void dictListToConfiguration( String value) {
        mModel.dictListToConfiguration(Api.APP_DOMAIN_ZIDIAN+"/app/dictList",value).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HouseInfoTitleBean.HouseInfoConfiguration>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HouseInfoTitleBean.HouseInfoConfiguration>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean))
                        {

                            mRootView.dictListToConfigurationSuccess(listResultBean.getData());
                        }else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }
                    }
                });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
